package com.yige.common;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sunhao on 2015/9/6 0006.
 */
public class OnlineManager {

    private static OnlineManager om;
    private Map<String,HttpSession> sessions;

    private OnlineManager(){
        sessions=new HashMap<String,HttpSession>();//为什么没有共用一个sessions;
    }


    public static OnlineManager getInstance(){
        if(om==null){
            om=new OnlineManager();
        }
        return om;
    }


    public void addSession(HttpSession se){
        String key=(String)se.getAttribute("username");
        sessions.put(key, se);
        System.out.println("add 1 : "+sessions.size());

    }
    public void removeSession(HttpSession se){
        String key=(String)se.getAttribute("username");
        //sessions.remove(key);  //这个只是把key=null
        sessions.remove(sessions.get(key));
        System.out.println("remove 1 : " +"key:"+key+sessions.size());
        System.out.println(sessions);
    }

}
